package com.android.lovingpet.community.posting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.common.PostModel;
import com.android.lovingpet.util.Utility;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PostingPresenter extends BasePresenter<PostingContract.View> implements PostingContract.Presenter {

    @Override
    public void getComments(int targetId, int offset) {
        PostModel.getInstance().getIdsByPost(targetId, offset, 10, new Back() {
            List<Post> comments = new ArrayList<>();
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                List<Integer> ids = bundle.getIntegerArrayList("ids");
                if (ids == null || ids.isEmpty()) {
                    getView().loadMoreComplete();
                    return;
                }
                for (int i : ids) {
                    PostModel.getInstance().getPost(i, 2, 1, new Back() {
                        @Override
                        public void onFailure(String msg) {
                            getView().showToast(msg);
                        }

                        @Override
                        public void onSuccess(@Nullable Bundle bundle) {
                            Post comment = bundle.getParcelable("post");
                            getUser(comment.getUserId());
                            comments.add(comment);
                            if (comments.size() == ids.size()) {
                                getView().loadMoreComplete();
                                getView().addComments(comments);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void comment(String context, int targetId) {
        if (TextUtils.isEmpty(context)) {
            return;
        }
        Post post = new Post();
        post.setContext(context);
        post.setTargetId(targetId);
        post.setCreateTime(SimpleDateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
        post.setContext(context);
        post.setUserId(Utility.getRealId());
        getView().addComment(post);
        getView().clearText();
        PostModel.getInstance().post(context, 0, targetId, 1, null, null, null, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {

            }
        });
    }

    private void getUser(int userId) {
        UserModel.getInstance().getInfo(userId, new FileModel.FileBack() {
            Account account;

            @Override
            public void loadSuccess(String url) {
                account.setHead(url);
                account.saveOrUpdate();
                getView().addCommentInfo();
            }

            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                account = bundle.getParcelable("account");
                account.saveOrUpdate();
                getView().addCommentInfo();
            }
        });
    }
}
